package com.example.netty.controller.all;

import com.example.netty.common.FieldClass;
import com.example.netty.common.FluxQuery;
import com.example.netty.common.R;
import com.example.netty.controller.BaseController;
import com.example.netty.pojo.*;
import com.example.netty.service.bak1.Bak1ServiceImpl;
import com.example.netty.service.bak1warning.Bak1ServiceWarningImpl;
import com.example.netty.service.bak2.Bak2ServiceImpl;
import com.example.netty.service.bak2warning.Bak2WarningServiceImpl;
import com.example.netty.service.gpspostitiondevice.GpsPostitionDeviceServiceImpl;
import com.example.netty.service.hyb.HybServiceImpl;
import com.example.netty.service.hybwarning.HybWarningServiceImpl;
import com.example.netty.service.hycyw.HycywServiceImpl;
import com.example.netty.service.hycywwarning.HycywWarningServiceImpl;
import com.example.netty.service.hymain.HyMainServiceImpl;
import com.example.netty.service.hymainwarning.HyMainWarningServiceImpl;
import com.example.netty.service.leftmain.LeftMainServiceImpl;
import com.example.netty.service.leftmainwarning.LeftMainWarningServiceImpl;
import com.example.netty.service.offside.OffsideServiceImpl;
import com.example.netty.service.power1.Power1ServiceImpl;
import com.example.netty.service.power1warning.Power1WarningServiceImpl;
import com.example.netty.service.power2.Power2ServiceImpl;
import com.example.netty.service.power2warning.Power2WarningServiceImpl;
import com.example.netty.service.power3.Power3ServiceImpl;
import com.example.netty.service.power3warning.Power3WarningServiceImpl;
import com.example.netty.service.powerbak.PowerBakServiceImpl;
import com.example.netty.service.powerbakwarning.PowerBakWarningServiceImpl;
import com.example.netty.service.rightmain.RightMainServiceImpl;
import com.example.netty.service.rightmainwarning.RightMainWarningServiceImpl;
import com.example.netty.service.shiplogdevice.ShiplogDeviceServiceImpl;
import com.example.netty.vo.*;
import com.influxdb.client.InfluxDBClient;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
public class AllController extends BaseController {
    @Autowired
    LeftMainServiceImpl leftMainService;
    @Autowired
    RightMainServiceImpl rightMainService;
    @Autowired
    Power1ServiceImpl power1Service;
    @Autowired
    Power2ServiceImpl power2Service;
    @Autowired
    Power3ServiceImpl power3Service;
    @Autowired
    PowerBakServiceImpl powerBakService;
    @Autowired
    HyMainServiceImpl hyMainService;
    @Autowired
    Bak1ServiceImpl bak1Service;
    @Autowired
    Bak2ServiceImpl bak2Service;
    @Autowired
    HycywServiceImpl hycywService;
    @Autowired
    HybServiceImpl hybService;
    @Autowired
    OffsideServiceImpl iOffsideService;
    @Autowired
    GpsPostitionDeviceServiceImpl gpsPostitionDeviceService;
    @Autowired
    ShiplogDeviceServiceImpl shiplogDeviceService;
    @Autowired
    LeftMainWarningServiceImpl leftMainWarningService;
    @Autowired
    RightMainWarningServiceImpl rightMainWarningService;
    @Autowired
    Power1WarningServiceImpl power1WarningService;
    @Autowired
    Power2WarningServiceImpl power2WarningService;
    @Autowired
    Power3WarningServiceImpl power3WarningService;
    @Autowired
    PowerBakWarningServiceImpl powerBakWarningService;
    @Autowired
    HyMainWarningServiceImpl hyMainWarningService;
    @Autowired
    Bak1ServiceWarningImpl bak1WarningService;
    @Autowired
    Bak2WarningServiceImpl bak2WarningService;
    @Autowired
    HycywWarningServiceImpl hycywWarningService;
    @Autowired
    HybWarningServiceImpl hybWarningService;


    @Autowired
    InfluxDBClient influxDBClient;


//    @GetMapping("/getAllHistoryList")
//    public R listAllFive(String time) throws Exception {
//        Map<String, Object> fmap = new HashMap<>();
//
//        FieldClass.fieldCompare(LeftMain.class, leftMainService.findOneData(time), leftMainWarningService.findOne(), fmap);
//
//        FieldClass.fieldCompare(RightMain.class, rightMainService.findOneData(time), rightMainWarningService.findOne(), fmap);
//
//        FieldClass.fieldCompare(Power1.class, power1Service.findOneData(time), power1WarningService.findOne(), fmap);
//
//        FieldClass.fieldCompare(Power2.class, power2Service.findOneData(time), power2WarningService.findOne(), fmap);
//
//        FieldClass.fieldCompare(Power3.class, power3Service.findOneData(time), power3WarningService.findOne(), fmap);
//
//        FieldClass.fieldCompare(PowerBak.class, powerBakService.findOneData(time), powerBakWarningService.findOne(), fmap);
//
//        FieldClass.fieldCompare(HyMain.class, hyMainService.findOneData(time), hyMainWarningService.findOne(), fmap);
//
//        FieldClass.fieldCompare(Bak1.class, bak1Service.findOneData(time), bak1WarningService.findOne(), fmap);
//
//        FieldClass.fieldCompare(Bak2.class, bak2Service.findOneData(time), bak2WarningService.findOne(), fmap);
//
//        FieldClass.fieldCompare(Hycyw.class, hycywService.findOneData(time), hycywWarningService.findOne(), fmap);
//
//        FieldClass.fieldCompare(Hyb.class, hybService.findOneData(time), hybWarningService.findOne(), fmap);
//
//        fmap.put("iOffside", iOffsideService.eightListData(time));
//        fmap.put("newiOffside", iOffsideService.newListData(time));
//        fmap.put("gpsPostitionDeviceService", gpsPostitionDeviceService.findOneData(time));
//        fmap.put("shiplogDeviceService", shiplogDeviceService.findOneData(time));
//        fmap.put("state",false);
//
//        return R.ok(fmap);
//    }

    @GetMapping("/getAllData")
    public R listAllFive(String bucketName, String start,String stop,String measurementName,String fields,String unit){
        List<FluxVO> data = FluxQuery.data(influxDBClient, bucketName, start, stop, measurementName, fields, unit);
        return R.ok().put("list",data);
    }
    @GetMapping("/getAll")
    public R listAll(String bucketName,String start,String stop,String measurementName,String fields){
        List<FluxAllVO> data = FluxQuery.all(influxDBClient, bucketName, start, stop, measurementName, fields);
        return R.ok().put("list",data);
    }
//    @GetMapping("/getAllMinute")
//    public R listAllMinute(String start,String stop,String measurementName,String fields,String unit){
//        List<FluxMVO> data = FluxQuery.dataMinute(influxDBClient,start, stop, measurementName, fields, unit);
//        return R.ok().put("list",data);
//    }

    @PostMapping("/postIntervalWind")
    public R listIntervalWind(@RequestBody FFVo ffVo){
        Integer offset=0;
        if(ffVo.getTime().equals(45)){
            offset=15;
        }
        List<FVOWind> list = FluxQuery.intervalWind(influxDBClient,ffVo.getBucketName(),ffVo.getStart(), ffVo.getStop(), ffVo.getMeasurementName(), ffVo.getFields(), 0, 1000, ffVo.getTime(), offset);
        Map<String, List<FVOWind>> collect = list.stream().collect(Collectors.groupingBy(FVOWind::getField, Collectors.toList()));
        return R.ok().put("list",collect);
    }
}
